/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SystemDataTypes;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.*;

import date.DateParser;

/**
 *
 * @author Administrador
 */
public class EventReader {
	
    public static Date readTimeStampFromJSONString(String strMessage){    	
    	JSONObject fobject = null;    	
        try {
            fobject = new JSONObject(strMessage);
            if(fobject.has("nOrden")) return DateParser.toDate(fobject.getString("nOrden"));                         
        } catch (JSONException ex) {
            Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        return null;
    }
    
    public static Object readFromJSONString(String strMessage){

    	System.out.println("EL MENSAJE FUE:" + strMessage);
       
        JSONObject fobject = null;
        JSONObject object = null;
        
        try {
            fobject = new JSONObject(strMessage);
            if(fobject.has("data")) fobject = new JSONObject(fobject.getString("data"));            
            
        } catch (JSONException ex) {
            Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex);
        }        
       
        try {
           
            if(fobject.has("Action"))
                return fobject.getString("Action");
            
            if(fobject.has("FlightPlan"))
                return new FlightPlan(fobject.getJSONObject("FlightPlan"));

            if(fobject.has("FlightData"))
                return new FlightData(fobject.getJSONObject("FlightData"));
            
            if(fobject.has("TowerEvent"))
                return new TowerEvent(fobject.getJSONObject("TowerEvent"));
          
            } catch (JSONException ex) {
                Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex);
                }
                
        return null;
    }
    
    public static Date readOrderFromJSONString(String strMessage){    
        JSONObject object = null;   
        try {
            object = new JSONObject(strMessage);
            if(object.has("nOrden")) return DateParser.toDate(object.getString("nOrden"));
        } catch (Exception ex) {
            Logger.getLogger(EventReader.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }    
    
}
